home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / util / netupdc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  3.4 KB  |  169 lines

  1. /* @(#) $Header: netupdc.c,v 1.5 91/10/25 14:21:26 deyke Exp $ */
  2.  
  3. /* Net Update Client */
  4.  
  5. #define _HPUX_SOURCE    1
  6.  
  7. #include <sys/types.h>
  8.  
  9. #include <fcntl.h>
  10. #include <netinet/in.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/socket.h>
  15. #include <sys/utsname.h>
  16. #include <unistd.h>
  17.  
  18. #if defined(__TURBOC__) || defined(__STDC__)
  19. #define __ARGS(x)       x
  20. #else
  21. #define __ARGS(x)       ()
  22. #define const
  23. #endif
  24.  
  25. #include "buildsaddr.h"
  26.  
  27. static void pexit __ARGS((const char *s));
  28. static void doread __ARGS((int fd, char *buf, size_t cnt));
  29. static void dowrite __ARGS((int fd, const char *buf, size_t cnt));
  30. int main __ARGS((int argc, char **argv));
  31.  
  32. /*---------------------------------------------------------------------------*/
  33.  
  34. static void pexit(s)
  35. const char *s;
  36. {
  37.   perror(s);
  38.   exit(1);
  39. }
  40.  
  41. /*---------------------------------------------------------------------------*/
  42.  
  43. static void doread(fd, buf, cnt)
  44. int  fd;
  45. char  *buf;
  46. size_t cnt;
  47. {
  48.  
  49.   char  *p = buf;
  50.   int  n;
  51.  
  52.   while (cnt) {
  53.     n = read(fd, p, cnt);
  54.     if (n < 0) pexit("read");
  55.     if (!n) {
  56.       printf("read(): End of file\n");
  57.       exit(1);
  58.     }
  59.     p += n;
  60.     cnt -= n;
  61.   }
  62. }
  63.  
  64. /*---------------------------------------------------------------------------*/
  65.  
  66. static void dowrite(fd, buf, cnt)
  67. int  fd;
  68. const char *buf;
  69. size_t cnt;
  70. {
  71.  
  72.   const char * p = buf;
  73.   int  n;
  74.  
  75.   while (cnt) {
  76.     n = write(fd, p, cnt);
  77.     if (n <= 0) pexit("write");
  78.     p += n;
  79.     cnt -= n;
  80.   }
  81. }
  82.  
  83. /*---------------------------------------------------------------------------*/
  84.  
  85. int  main(argc, argv)
  86. int  argc;
  87. char  **argv;
  88. {
  89.  
  90.   char  *client;
  91.   char  *server;
  92.   char  buf[1024];
  93.   char  filename[1024];
  94.   int  addrlen;
  95.   int  fdfile;
  96.   int  fdsocket;
  97.   int  filesize;
  98.   int  i;
  99.   int  net_filesize;
  100.   int  net_i;
  101.   struct sockaddr *addr;
  102.   struct utsname utsbuf;
  103.  
  104.   alarm(6 * 3600);
  105.  
  106.   if (uname(&utsbuf)) pexit("uname");
  107.  
  108.   server = (argc < 2) ? "db0sao" : argv[1];
  109.   client = (argc < 3) ? utsbuf.nodename : argv[2];
  110.  
  111.   if (getuid()) {
  112.     printf("%s: Permission denied\n", *argv);
  113.     exit(1);
  114.   }
  115.  
  116.   umask(022);
  117.   putenv("HOME=/users/root");
  118.   putenv("LOGNAME=root");
  119.   putenv("PATH=/bin:/usr/bin:/usr/local/bin:/usr/contrib/bin");
  120.   putenv("SHELL=/bin/sh");
  121.   putenv("TZ=MEZ-1MESZ");
  122.  
  123.   if (chdir("/tcp")) pexit("/tcp");
  124.  
  125.   if (!(addr = build_sockaddr("unix:/tcp/.sockets/netcmd", &addrlen))) {
  126.     printf("build_sockaddr(): Failed\n");
  127.     exit(1);
  128.   }
  129.  
  130.   fdsocket = socket(addr->sa_family, SOCK_STREAM, 0);
  131.   if (fdsocket < 0) pexit("socket");
  132.  
  133.   if (connect(fdsocket, addr, addrlen)) pexit("connect");
  134.  
  135.   strcpy(buf, "binary\n");
  136.   dowrite(fdsocket, buf, strlen(buf));
  137.  
  138.   sprintf(buf, "connect tcp %s 4715\n", server);
  139.   dowrite(fdsocket, buf, strlen(buf));
  140.  
  141.   dowrite(fdsocket, client, strlen(client) + 1);
  142.  
  143.   doread(fdsocket, (char *) &net_filesize, 4);
  144.   filesize = ntohl(net_filesize);
  145.  
  146.   tmpnam(filename);
  147.   fdfile = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  148.   if (fdfile < 0) pexit(filename);
  149.   while (filesize > 0) {
  150.     i = filesize < sizeof(buf) ? filesize : sizeof(buf);
  151.     doread(fdsocket, buf, (unsigned) i);
  152.     dowrite(fdfile, buf, (unsigned) i);
  153.     filesize -= i;
  154.   }
  155.   if (close(fdfile)) pexit("close");
  156.  
  157.   sprintf(buf, "uncompress < %s | sh", filename);
  158.   i = system(buf);
  159.   net_i = htonl(i);
  160.   dowrite(fdsocket, (char *) &net_i, 4);
  161.  
  162.   if (unlink(filename)) pexit(filename);
  163.  
  164.   if (!i) system("exec make");
  165.  
  166.   return i;
  167. }
  168.  
  169.